// 获取计算机PC的mac地址信息

import { ipcMain } from 'electron'
import { type NetworkInterfaceInfo, networkInterfaces } from 'os'
import { IPC } from 'root/common/constants'

/** 初始化给渲染进程使用的 ipc 方法 */
export function getPcMsg() {
  ipcMain.handle(IPC.PC_INFO, () => {
    let interfaces:NodeJS.Dict<NetworkInterfaceInfo[]> = networkInterfaces()
    let pcObj:any = {}
    let pcMessage = []
    for (let key in interfaces) {
      if(key.indexOf('WLAN') !== -1 || key.indexOf('无线网络连接') !== -1) {
        pcObj = interfaces[key]
        break
      } else if (key.indexOf('以太网') !== -1 || key.indexOf('本地连接') !== -1) {
        pcObj = interfaces[key]
      } else if (Object.keys(pcObj).length < 1) {
        pcObj = interfaces[key]
      }
    }
    pcMessage = pcObj.filter((item: { family: string }) => item.family === 'IPv4');
    return pcMessage[0]
  })
}